package org.jeecg.modules.oa.order.service.impl;

import org.jeecg.modules.oa.order.entity.MyOrderMain;
import org.jeecg.modules.oa.order.entity.MyOrderGoods;
import org.jeecg.modules.oa.order.entity.MyOrderCustomer;
import org.jeecg.modules.oa.order.mapper.MyOrderGoodsMapper;
import org.jeecg.modules.oa.order.mapper.MyOrderCustomerMapper;
import org.jeecg.modules.oa.order.mapper.MyOrderMainMapper;
import org.jeecg.modules.oa.order.service.IMyOrderMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 商城订单表
 * @Author: jeecg-boot
 * @Date:   2022-04-13
 * @Version: V1.0
 */
@Service
public class MyOrderMainServiceImpl extends ServiceImpl<MyOrderMainMapper, MyOrderMain> implements IMyOrderMainService {

	@Autowired
	private MyOrderMainMapper myOrderMainMapper;
	@Autowired
	private MyOrderGoodsMapper myOrderGoodsMapper;
	@Autowired
	private MyOrderCustomerMapper myOrderCustomerMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(MyOrderMain myOrderMain, List<MyOrderGoods> myOrderGoodsList,List<MyOrderCustomer> myOrderCustomerList) {
		myOrderMainMapper.insert(myOrderMain);
		if(myOrderGoodsList!=null && myOrderGoodsList.size()>0) {
			for(MyOrderGoods entity:myOrderGoodsList) {
				//外键设置
				entity.setOrderMainId(myOrderMain.getId());
				myOrderGoodsMapper.insert(entity);
			}
		}
		if(myOrderCustomerList!=null && myOrderCustomerList.size()>0) {
			for(MyOrderCustomer entity:myOrderCustomerList) {
				//外键设置
				entity.setOrderMainId(myOrderMain.getId());
				myOrderCustomerMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(MyOrderMain myOrderMain,List<MyOrderGoods> myOrderGoodsList,List<MyOrderCustomer> myOrderCustomerList) {
		myOrderMainMapper.updateById(myOrderMain);
		
		//1.先删除子表数据
		myOrderGoodsMapper.deleteByMainId(myOrderMain.getId());
		myOrderCustomerMapper.deleteByMainId(myOrderMain.getId());
		
		//2.子表数据重新插入
		if(myOrderGoodsList!=null && myOrderGoodsList.size()>0) {
			for(MyOrderGoods entity:myOrderGoodsList) {
				//外键设置
				entity.setOrderMainId(myOrderMain.getId());
				myOrderGoodsMapper.insert(entity);
			}
		}
		if(myOrderCustomerList!=null && myOrderCustomerList.size()>0) {
			for(MyOrderCustomer entity:myOrderCustomerList) {
				//外键设置
				entity.setOrderMainId(myOrderMain.getId());
				myOrderCustomerMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		myOrderGoodsMapper.deleteByMainId(id);
		myOrderCustomerMapper.deleteByMainId(id);
		myOrderMainMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			myOrderGoodsMapper.deleteByMainId(id.toString());
			myOrderCustomerMapper.deleteByMainId(id.toString());
			myOrderMainMapper.deleteById(id);
		}
	}
	
}
